package main

import (
	"encoding/json"

	"jlpay-sdk-go/pkg/jlpay/demo"
	"jlpay-sdk-go/pkg/jlpay/models/trans/c2b"
	"jlpay-sdk-go/pkg/jlpay/models/trans/dto"
	servicec2b "jlpay-sdk-go/pkg/jlpay/services/trans/c2b"
	"jlpay-sdk-go/pkg/jlpay/utils"
)

// ==================== 常量定义 ====================

// 支付类型常量
const (
	PAY_TYPE_WECHAT = "wxpay"    // 微信支付
	PAY_TYPE_ALIPAY = "alipay"   // 支付宝支付
	PAY_TYPE_UNION  = "unionpay" // 银联支付
)

// 构建终端信息数据
func buildTerminalInfo() *dto.TerminalInfoDto {
	terminalInfo := dto.NewTerminalInfoDto()
	terminalInfo.SetDeviceType("01")
	return terminalInfo
}

// 构建扫码支付预授权请求
func buildQrcodePayPreAuthRequest() *c2b.QrcodePayPreAuthRequest {
	request := c2b.NewQrcodePayPreAuthRequest()

	// 设置请求参数
	request.SetMchID("849584358120018")
	request.SetTermNo("60131234")
	request.SetPayType(PAY_TYPE_WECHAT)
	request.SetOutTradeNo("ld201911023465530")
	request.SetPaymentValidTime("10")
	request.SetBody("测试扫码预授权商品")
	request.SetAttach("测试扫码预授权描述")
	request.SetTotalFee("300")
	request.SetRemark("测试扫码预授权备注")
	request.SetNotifyURL("https://example.com/notify")
	request.SetOpUserID("test_user")
	request.SetOpShopID("GD_SZ_001")
	request.SetDeviceInfo("N50007A8890")
	request.SetMchCreateIP("127.0.0.1")
	request.SetTransAddress("北京市朝阳区")
	request.SetAreaCode("110105")
	request.SetLongitude("116.397128")
	request.SetLatitude("39.916527")
	request.SetSubAppid("wxid_129752pfasf1671")

	// 设置终端信息
	terminalInfo := buildTerminalInfo()
	request.SetTerminalInfo(terminalInfo)

	return request
}

// 扫码支付预授权示例
func qrcodePayPreAuthDemo(qrcodePayPreAuthService *servicec2b.QrcodePayPreAuthService) {
	// 创建扫码支付预授权请求
	request := buildQrcodePayPreAuthRequest()

	// 序列化请求数据并打印
	requestMap, _ := request.ToMap()
	requestJSON, _ := json.Marshal(requestMap)
	utils.Debugf("扫码支付预授权请求数据: %s", string(requestJSON))

	// 发送请求
	response, err := qrcodePayPreAuthService.QrcodePayPreAuth(request)
	if err != nil {
		utils.Errorf("扫码支付预授权请求失败: %v", err)
		return
	}

	// 处理响应 - 使用BaseResponse接口的HandleResponse方法
	response.HandleResponse("扫码支付预授权")
}

func main() {
	// 设置日志级别为info，显示详细的调试信息
	utils.SetLogLevel("info")

	utils.Info("=============================")
	utils.Info("===== 扫码支付预授权示例 开始 =====")
	utils.Info("=============================")

	// 创建配置对象和扫码支付预授权服务
	config := demo.CreateDemoConfig()
	qrcodePayPreAuthService := servicec2b.NewQrcodePayPreAuthService(config)

	qrcodePayPreAuthDemo(qrcodePayPreAuthService)

	utils.Info("=============================")
	utils.Info("===== 扫码支付预授权示例执行完毕 =====")
	utils.Info("=============================")
}
